﻿// <copyright file="ImageUriConverter.cs" company="Microsoft Corporation">
// Copyright (c) 2008 All Right Reserved
// </copyright>
// <author>Michael S. Scherotter</author>
// <email>mischero@microsoft.com</email>
// <date>2008-09-27</date>
// <summary>Convert a URI to a BitmapImage</summary>

namespace Photobucket
{
    using System;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Data;
    using System.Windows.Documents;
    using System.Windows.Ink;
    using System.Windows.Input;
    using System.Windows.Media;
    using System.Windows.Media.Animation;
    using System.Windows.Media.Imaging;
    using System.Windows.Shapes;

    /// <summary>
    /// Value converter for a string or URI to an image 
    /// </summary>
    public class ImageUriConverter : IValueConverter
    {
        /// <summary>
        /// Initializes a new instance of the ImageUriConverter class.
        /// </summary>
        public ImageUriConverter()
        {
        }

        /// <summary>
        /// Convert a string or URI to a BitmapImage
        /// </summary>
        /// <param name="value">The source data being passed to the target.</param>
        /// <param name="targetType">The Type of data expected by the target dependency property.</param>
        /// <param name="parameter">An optional parameter to be used in the converter logic.</param>
        /// <param name="culture">The culture of the conversion.</param>
        /// <returns>The value to be passed to the target dependency property.</returns>
        public object Convert(
            object value, 
            Type targetType, 
            object parameter, 
            System.Globalization.CultureInfo culture)
        {
            if (value == null)
            {
                return null;
            }

            string stringValue = value as string;

            if (stringValue != null)
            {
                return new BitmapImage(new Uri(stringValue));
            }

            Uri uri = value as Uri;

            if (uri != null)
            {
                return new BitmapImage(uri);
            }

            return null;
        }

        /// <summary>
        /// Convert from a BitmapImage to a URI
        /// </summary>
        /// <param name="value">The target data being passed to the source.</param>
        /// <param name="targetType">The Type of data expected by the source object.</param>
        /// <param name="parameter">An optional parameter to be used in the converter logic.</param>
        /// <param name="culture">The culture of the conversion.</param>
        /// <returns>The value to be passed to the source object.</returns>
        public object ConvertBack(
            object value, 
            Type targetType, 
            object parameter, 
            System.Globalization.CultureInfo culture)
        {
            BitmapImage image = value as BitmapImage;

            if (targetType == typeof(Uri))
            {
                return image.UriSource;
            }

            return null;
        }
    }
}

